import { createRouter, createWebHistory } from 'vue-router'
import Index from '../views/main/Index.vue'
import Home from '../views/main/Home.vue'
import About from '../views/main/About.vue'
import Result from '../views/main/result.vue'
import Login from '../views/main/Login.vue'
import User from '../views/main/user.vue'

import AdminIndex from '../views/admin/Index.vue'
import Admin from '../views/admin/Admin.vue'
import Teacher from '../views/admin/teacher.vue'
import Register from '../views/admin/register.vue'
import Flies from '../views/admin/Flies.vue'
import AdminLogin from '../views/admin/Login.vue'

const routes = [{
  path: '/',
  name: 'Index',
  component: Index,
  children: [{
    path: '/',
    name: 'Home',
    component: Home,
  },
  {
    path: '/about',
    name: 'About',
    component: About
  },
  {
    path: '/result',
    name: 'Result',
    component: Result
  },
  ]
},
{
  path: '/admin',
  name: 'adminIndex',
  component: AdminIndex,
  children: [{
    path: '/admin',
    name: 'Admin',
    component: Admin
  },
  {
    path: '/admin/student/register',
    name: 'Register',
    component: Register
  }, {
    path: '/admin/student/flies',
    name: 'Flies',
    component: Flies
  },
  {
    path: '/admin/teacher',
    name: 'Teacher',
    component: Teacher
  },
  {
    path: '/admin/adminLogin',
    name: 'AdminLogin',
    component: AdminLogin
  }
  ]
},

{
  path: '/login',
  name: 'Login',
  component: Login
},
{
  path: '/user',
  name: 'User',
  component: User,
},


]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  let token = JSON.parse(localStorage.getItem("token"));
  // let admintoken = JSON.parse(localStorage.getItem("admintoken"));
  // console.table(token)
  // console.table(admintoken)
  // if (to.path === '/admin') {
  //     next()
  // }
  if (token || to.path === '/login') {
    next()
  } else {
    next('/login')
  }
  // if (admintoken || to.path === '/admin/adminLogin') {
  //     next()
  //     console.log('111111')
  // } else {
  //     next('/admin/adminLogin')
  //     console.log('222222222')
  // }
})

export default router